const path = require("path");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const webpack = require("webpack");

module.exports = {
    entry: {
        index : path.resolve(__dirname, "../src/js/index.js"),
    },
    output: {
        filename : "./js/[name].[contenthash:8].js",
        // path : path.resolve(__dirname, "../src/html")
        path : path.resolve(__dirname, "../../../public/me_union")
    },
    plugins : [
        new webpack.HashedModuleIdsPlugin(),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            jquery: "jquery",
            "window.$":"jquery",
            "window.jQuery":"jquery"
        }),
        new CleanWebpackPlugin(["me_union"], {
            root: path.resolve(__dirname, "../../../public")
        }),
        new MiniCssExtractPlugin({
            filename : "[name].[contenthash:8].css",
            chunkFilename : "[name].[contenthash:8].css"
        }),

        new HtmlWebpackPlugin({
            template : path.resolve(__dirname, "../src/pages/index/index.js"),
            filename : "index.html",
            chunks : ["index"]
        }),
        
    ],
    module : {
        rules : [
            {
                test : /\.css$/,
                use : [MiniCssExtractPlugin.loader, "css-loader", "postcss-loader"]
            },
            {
                test: /\.ejs$/,
                exclude : /node_modules/,
                loader: "ejs-loader",
            },
            
            {
                loader: "image-webpack-loader"
            },
            {
                // 图片加载器，雷同file-loader，更适合图片，可以将较小的图片转成base64，减少http请求
                // 如下配置，将小于8192byte的图片转成base64码
                test: /\.(png|jpe?g|gif)$/,
                include: path.resolve(__dirname, "../src/imgs"),
                // loader: "url-loader?limit=8192&name=./static/img/[hash].[ext]",
                loader: "url-loader",
                options: {
                    limit: 8192,
                    name: "./static/img/[hash].[ext]",
                },
            },
        ],
        
    }
};